package org.hjq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.hjq.entity.Info;
import org.hjq.entity.LdapUser;
import org.hjq.entity.User;
import org.hjq.mapper.UserMapper;
import org.hjq.service.IUserService;
import org.hjq.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private LDAPService ldapService;
    @Override
    public User queryById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public Info login(String username, String password,Integer type) {
        // 数据库登陆
        if(type == 1){
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw
                    .eq(User::getUsername,username)
                    .eq(User::getPassword,password);
            User user = baseMapper.selectOne(lqw);
            if (ObjectUtils.isEmpty(user)) {
                return null;
            }
            String token = jwtUtil.generateToken(user);
            return new Info(user.getId(),username,token);
        }
        // LDAP登陆
        if(type == 2){
            LdapUser user = null;
            System.out.println(ldapService.findByCn(username));
//            for (String str : ldapTemplate.list("cn=demo,dc=example,dc=com")) {
//                System.out.println(str);
//            }

        }
        // github登陆
        if(type == 3){

        }
        return null;
    }
}